package com.gywj.race.config.filter;

import com.gywj.race.entity.Employees;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
@Configuration
@WebFilter(filterName = "commonDataFilter", urlPatterns = "/*")
public class CommonDataFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("过滤器初始化");
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletResponse resp = (HttpServletResponse)response;
        HttpServletRequest req = (HttpServletRequest)request;
        HttpSession session = req.getSession();
        Employees employee = (Employees)session.getAttribute("employee");
        String uri = req.getRequestURI();
        //简单判断缓存中是否有用户
        if(employee==null){//没有用户
            //判断用户是否是选择跳到登录界面
            if(uri.endsWith("login-1.html")
                    ||uri.endsWith("/part1.html")
                    ||uri.endsWith("/part2.html")
                    ||uri.endsWith("/swagger-ui.html")
                    ||uri.endsWith("/success.html")
                    ||uri.endsWith("/failed.html")
                    ||uri.endsWith("/upload.html")
                    ||uri.endsWith("/teamAdd.html")
                    ||uri.endsWith("/race-index.html")
                    ||uri.endsWith("/certificate.html")
                    ||uri.endsWith("/numberbook.html")
                    ||uri.endsWith("/login")
                    ||uri.endsWith("/login/page")
                    ||uri.endsWith("/getVerify")
                    ||uri.endsWith("css")
                    ||uri.endsWith("js")
                    ||uri.endsWith("jpg")
                    ||uri.endsWith("png")
                    ||uri.endsWith("gif")
                    ||uri.endsWith("ico")
                    ||uri.endsWith("woff2")
                    ||uri.endsWith("/getVerify")
                    ||uri.endsWith("/participant/page/param/get/by/eventId")
                    ||uri.endsWith("/download/allNumberBook/by/eventId/online/status")
                    ||uri.endsWith("/participant/team/list/by/eventId")
                    ||uri.endsWith("/participant/numberbook/create")
                    ||uri.endsWith("/participant/race/certificate")
                    ||uri.endsWith("/event/information/byId")
                    ||uri.endsWith("/event/information/isAble")
                    ||uri.endsWith("/get/race/group/part/code")
                    ||uri.endsWith("/list")
                    ||uri.endsWith("/up")
                    ||uri.endsWith("/getPage2Info")
                    ||uri.endsWith("/send/sms")
                    ||uri.endsWith("/wxpay")
                    ||uri.endsWith("/wxpay/notice")
                    ||uri.endsWith("/ui")
                    ||uri.endsWith("/swagger-resources")
                    ||uri.endsWith("/v2/api-docs")
                    ||uri.endsWith("/getAccessToken")
                    ||uri.endsWith("/participant/team/add")
                    ||uri.endsWith("/wx/auth")
                    ||uri.endsWith("/wx/getOpenIdByCode")
                    ||uri.endsWith("/wx/getTicket")
                    ||uri.endsWith("/wx/getSignature")
                    ||uri.endsWith("/cancelOrder")
                    ||uri.endsWith("/putDataBase64Jpg")
                    ||uri.endsWith("/participant/pay/zero")
                    ||uri.endsWith("/participant/all/data/by/openId/eventId")
                    ||uri.endsWith("/participant/update/best/score/pic")

                    ){
                chain.doFilter(request, response);
            }else{
                System.out.println("过滤器过滤");
                System.out.println(uri);
                resp.sendRedirect(req.getContextPath()+"/login/page");
            }
        }else{//有用户
            chain.doFilter(request, response);
        }
    }

    @Override
    public void destroy() {
        System.out.println("过滤器销毁");
    }
}
